package com.naiterui.ehp.bs.cms.modules.base.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.naiterui.ehp.bp.bo.cms.AgreementBO;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.cms.common.constants.DsName;
import com.naiterui.ehp.bs.cms.common.exception.BusinessExceptionCode;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.base.entity.Agreement;
import com.naiterui.ehp.bs.cms.modules.base.mapper.AgreementBaseMapper;
import com.naiterui.ehp.bs.cms.modules.base.service.IAgreementService;
import com.naiterui.ehp.bs.cms.modules.base.vo.AgreementPageVO;
import com.naiterui.ehp.bs.cms.modules.base.vo.AgreementVO;

import cn.hutool.core.bean.BeanUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 协议管理
 * 
 * @author yangfuya
 * @since 2021/10/12
 */
@Slf4j
@Service
@DS(DsName.EHP)
@AllArgsConstructor
public class AgreementServiceImpl implements IAgreementService {
    private final AgreementBaseMapper agreementMapper;

    @Override
    public PageVO<AgreementPageVO> queryPage(String title, PageParamsVO pageParamsVO) {
        QueryWrapper<AgreementPageVO> entityWrapper = buildEntityWrapper(pageParamsVO, title);
        Page<AgreementPageVO> page = agreementMapper.getPage(pageParamsVO.getPage(), entityWrapper);
        return PageVO.toPageVo(page);
    }

    @Override
    public AgreementVO getById(Long id) throws BusinessException {
        Agreement agreement = agreementMapper.selectById(id);
        if (agreement == null) {
            throw new BusinessException(BusinessExceptionCode.PARAM_ERROR);
        }
        return BeanUtil.copyProperties(agreement, AgreementVO.class);
    }

    @Override
    public void saveOrUpdate(AgreementVO agreementVO) throws BusinessException {
        Integer id = agreementVO.getId();
        if (id == null) {
            agreementMapper.insert(BeanUtil.copyProperties(agreementVO, Agreement.class));
        } else {
            Agreement agreement = agreementMapper.selectById(id);
            if (agreement == null) {
                throw new BusinessException(BusinessExceptionCode.PARAM_ERROR);
            }
            agreement.setType(agreementVO.getType());
            agreement.setTitle(agreementVO.getTitle());
            agreement.setContent(agreementVO.getContent());
            agreement.setRemarks(agreementVO.getRemarks());
            agreement.setStatus(agreementVO.getStatus());
            agreementMapper.updateById(agreement);
        }
    }

    @Override
    public void deleteById(Long id) throws BusinessException {
        Agreement agreement = agreementMapper.selectById(id);
        if (agreement == null) {
            throw new BusinessException(BusinessExceptionCode.PARAM_ERROR);
        }
        agreement.setDelFlag(1);
        agreementMapper.updateById(agreement);
    }

    @Override
    public AgreementBO getByType(String type) throws BusinessException {
        Agreement agreement = agreementMapper
            .selectOne(new QueryWrapper<Agreement>().lambda().eq(Agreement::getType, type).eq(Agreement::getStatus, 0)
                .eq(Agreement::getDelFlag, 0).orderByDesc(Agreement::getCreatedAt).last("limit 1"));
        if (agreement == null) {
            throw new BusinessException(BusinessExceptionCode.PARAM_ERROR);
        }
        return AgreementBO.builder().id(agreement.getId()).type(agreement.getType()).title(agreement.getTitle())
            .content(agreement.getContent()).remarks(agreement.getRemarks()).build();
    }

    private QueryWrapper<AgreementPageVO> buildEntityWrapper(PageParamsVO pageParamsVO, String title) {
        QueryWrapper<AgreementPageVO> entityWrapper = pageParamsVO.getEntityWrapper("t");
        entityWrapper.like(StringUtils.isNotBlank(title), "t.title", title);
        entityWrapper.eq("t.del_flag", 0);
        entityWrapper.orderByDesc("t.created_at");
        return entityWrapper;
    }
}
